package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.constant.PermissionConstant;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.common.util.PermissionUtil;
import com.spice.service.entity.pojo.User;
import com.spice.service.mapper.UserMapper;
import com.spice.service.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    public Response saveUser(User newUser) {
        // 确认必要参数
        if (CheckUtil.objectsHasNull(
                newUser.getUsername(),
                newUser.getPassword(),
                newUser.getNickname())) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        } else {
            // 确认账号或密码是否符合规范
            if (newUser.checkUsernameAndPassword()) {
                // 加密后保存
                newUser.encryptPassword();
            } else {
                throw new SpiceException().message("账号或密码不符合规范");
            }
            // 确认角色是否规范
            if (!PermissionUtil.isLegalRoleExceptRoot(newUser.getStatus())) {
                // 不符合规范
                throw new SpiceException().message("角色不符合规范 异常角色或是root");
            }

            // 确认用户名是否已经存在
            if (this.count(new LambdaQueryWrapper<User>().eq(User::getUsername, newUser.getUsername())) > 0) {
                throw new SpiceException().message("账号已存在");
            }
            // 是否保存成功
            if (this.save(newUser)) {
                return Response.ok().message("添加用户成功").data("user", newUser.desensitization());
            } else {
                throw new SpiceException().message("添加用户失败");
            }

        }
    }

    public Response removeUser(Long id) {
        // 确认必要参数
        if (CheckUtil.objectsHasNull(id)) {
            throw new SpiceException(ResultCodeEnum.PARAM_LACK);
        } else {
            // 确认该用户是否存在
            User removedUser = this.getById(id);
            if (CheckUtil.objectsHasNull(removedUser.getId())) {
                throw new SpiceException().message("该用户id不存在");
            }
            // 是否删除成功
            if (this.removeById(id)) {
                return Response.ok().message("删除用户成功").data("user", removedUser.desensitization());
            } else {
                throw new SpiceException().message("删除用户失败");
            }
        }
    }

    public Response updateUser(Long id, User newUser) {
        // 确认参数必要参数
        if (CheckUtil.objectsHasNull(id, newUser)) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        } else {
            if (this.count(new LambdaQueryWrapper<User>().eq(User::getId, id)) == 0) {
                throw new SpiceException().message("该用户id不存在");
            }
            User user = this.getById(id);
            // 确认账号是否符合规范
            if (!CheckUtil.objectsHasNull(newUser.getPassword())) {
                if (!newUser.checkUsername()) {
                    // 不为空且不符合规范
                    throw new SpiceException().message("账号不符合规范");
                }
                if (this.count(new LambdaQueryWrapper<User>()
                        .eq(User::getUsername, newUser.getUsername())) > 0) {
                    throw new SpiceException().message("该用户名已被注册");
                }
                user.setUsername(newUser.getPassword());
            }
            // 确认密码是否符合规范
            if (!CheckUtil.objectsHasNull(newUser.getPassword())) {
                // 不为空
                if (newUser.checkPassword()) {
                    newUser.encryptPassword();
                } else {
                    throw new SpiceException().message("密码不符合规范");
                }
                user.setPassword(newUser.getPassword());
            }
            // 确认角色是否规范
            if (!CheckUtil.objectsHasNull(newUser.getStatus())) {
                // 不为空且不符合规范
                if (PermissionUtil.isLegalRoleExceptRoot(newUser.getStatus())) {
                    user.setStatus(newUser.getStatus());
                } else {
                    throw new SpiceException().message("角色不符合规范");
                }
            }
            if (!CheckUtil.objectsHasNull(newUser.getAvatar())){
                user.setAvatar(newUser.getAvatar());
            }

            // 是否更新成功
            if (this.updateById(user)) {
                return Response.ok().message("更新用户成功").data("user", newUser.desensitization());
            } else {
                throw new SpiceException().message("更新用户失败");
            }
        }
    }
}
